package com.lishe.lishe.lishe.module.mall.view.snapscrollview;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import com.lishe.lishe.R;

/**
 * Created by Bin
 */
public class GoodsInfoPage implements SnapPageLayout.SnapPage {

	private Context context;

	private View rootView = null;
	private SnapScrollView mSnapScrollView = null;

	public GoodsInfoPage(Context context, View rootView) {
		this.context = context;
		this.rootView = rootView;
		mSnapScrollView = this.rootView.findViewById(R.id.snap_scrollview);
		mSnapScrollView.setOnJDScrollListener(new SnapScrollView.OnJDScrollListener() {
			@Override
			public void onScroll(int x, int y, int oldx, int oldy) {

			}
		});

		/******************** 监听ScrollView滑动停止 *****************************/
		mSnapScrollView.setOnTouchListener(new OnTouchListener() {
			private int lastY = 0;
			private int touchEventId = -9983761;
			Handler handler = new Handler() {
				@Override
				public void handleMessage(Message msg) {
					super.handleMessage(msg);
					View scroller = (View) msg.obj;
					if (msg.what == touchEventId) {
						if (lastY == scroller.getScrollY()) {

						} else {
							handler.sendMessageDelayed(handler.obtainMessage(touchEventId, scroller), 5);
							lastY = scroller.getScrollY();
						}
					}
				}
			};

			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);
				}
				return false;
			}
		});
	}

	public void go2top() {
		mSnapScrollView.scrollTo(0, 0);
	}

	@Override
	public View getRootView() {
		return rootView;
	}

	@Override
	public boolean isAtTop() {
		return true;
	}

	@Override
	public boolean isAtBottom() {
		int scrollY = mSnapScrollView.getScrollY();
		int height = mSnapScrollView.getHeight();
		int scrollViewMeasuredHeight = mSnapScrollView.getChildAt(0).getMeasuredHeight();

		if ((scrollY + height) >= scrollViewMeasuredHeight) {
			return true;
		}
		return false;
	}

}
